package com.gitee.wsl.struct.either

import arrow.core.NonEmptyList
import arrow.core.Option
import arrow.core.raise.option

/**
 * Map a function that returns an Option across the NonEmptyList.
 *
 * The first None result from calling the function will be the result, or if no calls result in a None
 * the result will be a Some(NonEmptyList) of all the B's returned.
 */
inline fun <A, B> NonEmptyList<A>.traverse(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
    let { nel -> option { nel.map { f(it).bind() } } }

/**
 * Synonym for traverse((A)-> Option<B>): Option<NonEmptyList<B>>
 */
inline fun <A, B> NonEmptyList<A>.traverseOption(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
    traverse(f)